package com.wizardClient;

import Validator.Validator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.CarsModel;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

/**
 *
 * @author Sawior
 */
public class ChooseCarController implements Initializable, ClientControlledScreen {

    private ClientScreenController myController;
    @FXML
    private TextField plate;
    @FXML
    private TextField make;
    @FXML
    private TextField model;
    @FXML
    private ComboBox carType;
    @FXML
    private TextArea comment;
    @FXML
    private Label labelPlate;
    @FXML
    private Label labelMake;
    @FXML
    private Label labelModel;
    @FXML
    private Label labelCarType;
    @FXML
    private Label labelComment;
    private ArrayList<Node> inputs = new ArrayList<Node>();
    private ArrayList<Label> labels = new ArrayList<Label>();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.add(plate);
        inputs.add(make);
        inputs.add(model);
        inputs.add(carType);
        inputs.add(comment);

        labels.add(labelPlate);
        labels.add(labelMake);
        labels.add(labelModel);
        labels.add(labelCarType);
        labels.add(labelComment);
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void goToMain(ActionEvent event) {
        myController.setScreen(ClientScreensFramework.FIRST_SCREEN);
    }

    public void loadData(Integer carId) {
        CarsModel car = DaoInstanceEnum.INSTANCE.getDao().getCarById(carId);
        if (car != null) {
            this.plate.setText(car.getNumberPlate());
            this.model.setText(car.getModel());
            this.make.setText(car.getMake());
            this.carType.setValue(renameCarTypeEnglish(car.getCarType()));
            this.comment.setText(car.getComments());
        }
    }

    @Override
    public boolean validateForm() {
        List<Boolean> checks = Validator.checkCarCheckList(getCar());
        setWrongTextFields(checks);
        return isCorrect(checks);
    }

    public CarsModel getCar() {
        String numberPlate = this.plate.getText();
        String make = this.make.getText();
        String model = this.model.getText();
        String carType = renameCarType((String) this.carType.getValue());
        String comments = this.comment.getText();
        CarsModel car = new CarsModel(numberPlate, make, model, carType);
        car.setComments(comments);

        return car;
    }

    private String renameCarType(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Osobowy")) {
            return "Personal";
        }
        if (polish.equals("Dostawczy")) {
            return "Van";
        }
        if (polish.equals("Motocykl")) {
            return "Motorcycle";
        }
        if (polish.equals("Przyczepka")) {
            return "Trailer";
        }
        return polish;
    }
    
    private String renameCarTypeEnglish(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Personal")) {
            return "Osobowy";
        }
        if (polish.equals("Van")) {
            return "Dostawczy";
        }
        if (polish.equals("Motorcycle")) {
            return "Motocykl";
        }
        if (polish.equals("Trailer")) {
            return "Przyczepka";
        }
        return polish;
    }

    public void setWrongTextFields(List<Boolean> textFieldsChecks) {
        int i = 0;
        for (Boolean isGood : textFieldsChecks) {
            if (isGood) {
                inputs.get(i).getStyleClass().remove("textFieldWrong");
                inputs.get(i).getStyleClass().add("textField");
                labels.get(i).setText("");
            } else {
                inputs.get(i).getStyleClass().remove("textField");
                inputs.get(i).getStyleClass().add("textFieldWrong");
                labels.get(i).setText("Złe dane");
            }
            i++;
        }
    }

    private boolean isCorrect(List<Boolean> list) {
        for (Boolean el : list) {
            if (!el) {
                return false;
            }
        }
        return true;
    }

    public void clearData() {
        this.plate.setText("");
        this.model.setText("");
        this.make.setText("");
        this.carType.setValue("");
        this.comment.setText("");
    }
}